package oracle.apps.financials.collection.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.cyrilmottier.android.greendroid.R;

public class ListOfValueActivity extends Activity {

	TextView tv = null;
	ListView lv = null;
	Button btn_selectAll = null;
	Button btn_inverseSelect = null;
	Button btn_calcel = null;
//	String name[] = new String[] { "Vision Operations", "Vision Operations (USA)", "Vision Foods", "Vision XY",
//			"Vision ABC", "Vision Operations ABC" };
	String name[] = new String[] { "Global Business Services", "Global Contract Services",
			"Global Quote To Order", "Revenue Operations", "Human Resources" };

	ArrayList<String> listStr = null;
	private List<HashMap<String, Object>> list = null;
	private MyAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.list_view);
		tv = (TextView) this.findViewById(R.id.tv);
		lv = (ListView) this.findViewById(R.id.lv);
		btn_selectAll = (Button) this.findViewById(R.id.selectall);
		btn_inverseSelect = (Button) this.findViewById(R.id.inverseselect);
		btn_calcel = (Button) this.findViewById(R.id.cancel);

		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ViewHolder vHollder = (ViewHolder) view.getTag();
				vHollder.cb.toggle();
				adapter.isSelected.put(position, vHollder.cb.isChecked());
			}
		});
		showCheckBoxListView();

	}

	public void showCheckBoxListView() {
		list = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < name.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("item_tv", name[i]);
			map.put("item_cb", false);
			list.add(map);

			adapter = new MyAdapter(this, list, R.layout.list_view_item, new String[] { "item_tv", "item_cb" },
					new int[] { R.id.item_textView, R.id.item_checkBox });
			lv.setAdapter(adapter);
			listStr = new ArrayList<String>();

		}
	}

	class MyAdapter extends BaseAdapter {
		public HashMap<Integer, Boolean> isSelected;
		private Context context = null;
		private LayoutInflater inflater = null;
		private List<HashMap<String, Object>> list = null;
		private String keyString[] = null;
		private String itemString = null;
		private int idValue[] = null;

		public MyAdapter(Context context, List<HashMap<String, Object>> list, int resource, String[] from, int[] to) {
			this.context = context;
			this.list = list;
			keyString = new String[from.length];
			idValue = new int[to.length];
			System.arraycopy(from, 0, keyString, 0, from.length);
			System.arraycopy(to, 0, idValue, 0, to.length);
			inflater = LayoutInflater.from(context);
			init();
		}

		public void init() {
			isSelected = new HashMap<Integer, Boolean>();
			for (int i = 0; i < list.size(); i++) {
				isSelected.put(i, false);
			}
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int position, View view, ViewGroup arg2) {
			ViewHolder holder = null;
			if (holder == null) {
				holder = new ViewHolder();
				if (view == null) {
					view = inflater.inflate(R.layout.list_view_item, null);
				}
				holder.tv = (TextView) view.findViewById(R.id.item_textView);
				holder.cb = (CheckBox) view.findViewById(R.id.item_checkBox);
				view.setTag(holder);
			}
			HashMap<String, Object> map = list.get(position);
			if (map != null) {
				itemString = (String) map.get(keyString[0]);
				holder.tv.setText(itemString);
			}
			holder.cb.setChecked(isSelected.get(position));
			return view;
		}

	}

	 class ViewHolder {
		public TextView tv = null;
		public CheckBox cb = null;
	}
}
